package com.example.moviecms2.controller;

import com.example.moviecms2.domain.Actor;
import com.example.moviecms2.repository.ActorRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 杨友奇
 * @Time 2018/4/19
 * 负责演员个人信息页面的修改
 */
@Controller
@RequestMapping(value = "/actor")
public class ActorController {
    @Autowired
    private ActorRepository actorRepository;

    //修改演员个人信息
    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String toEdit(Model model, @PathVariable Long id) {
        Actor actor = actorRepository.findOne(id);
        model.addAttribute("info", actor);
        return "actor/edit";
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public String doEdit(@RequestParam Long id,
                         @RequestParam String actor_name,
                         @RequestParam Integer sex,
                         @RequestParam String born) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        Date date = null;
        try {
            date = format.parse(born);
        } catch (Exception e) {
        }
        //获取ID
        Actor actor = actorRepository.findOne(id);
        //设置数值
        actor.setName(actor_name);
        actor.setSex(sex);
        actor.setBorn(date);
        //更新变量
        actorRepository.save(actor);

        return "redirect:/movie/index";
    }
}
